﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

using VirtualTournaments.DataAccess.SqlServer.DataMappers;

namespace VirtualTournaments.DataAccess.SqlServer.DataProxies
{
	internal sealed class TeamDataProxy : Business.Tournaments.Team, IDataProxy<SqlServer.Team>, Identifiable<SqlServer.Team>
	{
		internal TeamDataProxy(SqlServer.Team dataObject)
			: base(dataObject.Name)
		{
			InnerDataObject = dataObject;
			MapProperties();
		}

		#region Override properties

		internal bool PlayersHasBeenRead { get; set; }
		public override IEnumerable<Business.Players.Player> Players
		{
			get
			{
				if (!PlayersHasBeenRead)
				{
					PlayersHasBeenRead = true;
					if (base.Players == null)
					{
						base.Players = MapPlayers();
					}
				}
				return base.Players;
			}
		}

		#endregion

		#region Mapping methods

		private void MapProperties()
		{
		}

		private IEnumerable<Business.Players.Player> MapPlayers()
		{
			return InnerDataObject.Players.Select(player => PlayersDataMapper.Instance.DataObject2BusinessObject(player)).AsEnumerable();
		}

		#endregion

		#region IDataProxy<Team> Members

		public SqlServer.Team InnerDataObject { get; set; }

		#endregion

		#region Identifiable<Team> Members

		public Expression<Func<SqlServer.Team, bool>> GetUniqueWhereExpression()
		{
			return team => team.TeamId == InnerDataObject.TeamId;
		}

		#endregion

		public override bool Equals(object obj)
		{
			//TODO: implementar y validar que se trata de la misma referencia, o el mismo proxy, o el mismo nombre
			throw new NotImplementedException();
		}

		public override int GetHashCode()
		{
			return base.GetHashCode();
		}
	}
}
